#include "AI.h"
#include "GameState.h"
#include "Sprite.h"

std::map<Sprite *, TimerTickAnimator *> AI::timer;
Popeye * AI::popeye;

AI::AI(void) {}

AI::~AI(void) {}
animid_t id=0;

void AI::Register(Sprite *s, TickFunc f, delay_t delay, int repetitions) {
	TickAnimation *anim = new TickAnimation(id++, delay, repetitions, f, s);
	TimerTickAnimator *timerTick = new TimerTickAnimator(anim);

	timer.insert(pair<Sprite *, TimerTickAnimator *>(s, timerTick));
	AnimatorHolder::Register(timerTick);
}

void AI::StartAI(void) {
	std::map<Sprite *, TimerTickAnimator *>::iterator it;

	for(it=timer.begin(); it!=timer.end(); ++it) {
		(*it).second->Start(Timer::getgametime());
		AnimatorHolder::MarkAsRunning((*it).second);
	}
}

void AI::SetFinish(Sprite *s, Animator::FinishCallback f) {
	std::map<Sprite *, TimerTickAnimator *>::iterator it = timer.find(s);
	(*it).second->SetOnFinish(f, s);
}

void AI::StopAnimator(Sprite *s) {
	std::map<Sprite *, TimerTickAnimator *>::iterator it = timer.find(s);
	(*it).second->Stop();
	AnimatorHolder::DelayedSuspention((*it).second);
	timer.erase(it);
}

void AI::setPopeye(Popeye *p) {
	popeye = p;
}

Rect AI::getPopeyeRect(void) {
	return popeye->GetRect();
}

Platform *AI::getPopeyePlatform(void) {
	return (Platform *) popeye->GetPlatform();
}

enum popeyeMode AI::getPopeyeMode(void) {
	return popeye->getMode();
}

bool AI::getPopeyeDirection(void) {
	return popeye->GetFlipH();
}

void AI::PopeyeDies(void) {
	popeye->Die();
}
	